package com.icehand.canal.handler;

import com.google.common.collect.Lists;
import com.icehand.canal.annotation.HandlePoint;
import com.icehand.canal.client.Point;
import lombok.Getter;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.reflect.Method;
import java.util.List;

/**
 * @author icehand
 */
@Getter
public class HandlerWrapper {
    private List<CanalHandler> handlers = Lists.newArrayList();
    private List<Point> points = Lists.newArrayList();

    public HandlerWrapper addHandler(CanalHandler canalHandler){
        handlers.add(canalHandler);
        return this;
    }

    public HandlerWrapper addPoint(Point point){
        points.add(point);
        return this;
    }

    public HandlerWrapper addPoint(Object object) {
        Method[] methods = object.getClass().getDeclaredMethods();
        if (methods != null && methods.length > 0) {
            for (Method method : methods) {
                HandlePoint l = AnnotationUtils.findAnnotation(method, HandlePoint.class);
                if (l != null) {
                    points.add(new Point(object, method, l));
                }
            }
        }
        return this;
    }
}
